{include file="public/header" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="expand">全部展开</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="fold">全部折叠</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="insert">添加</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        {{#  if(d.parent_id != 0){ }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
        {{#  } else { }}
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">顶级岗位禁止编辑</a>
        {{#  } }}
      </script>
    </div>
  </div>
</div>
<script>
  layui.config({
    base: '__PLUGIN__/treetable-lay/module/'
    ,version: new Date().getTime()
  }).extend({
    treetable: 'treetable-lay/treetable',
  }).use(['table','element', 'treetable'], function(){
    var $ = layui.jquery;
    var table = layui.table
            ,treetable = layui.treetable
            ,element = layui.element
            ,form = layui.form;
    var renderTable = function () {
      layer.load(2);
      treetable.render({
        elem: '#LAY-app-content-list',
        treeColIndex: 1,
        treeSpid: 0,
        treeIdName: 'id',
        treePidName: 'parent_id',
        treeDefaultClose:true,
        treeLinkage:true,
        url: "{:url('job/getJobList')}",
        cols: [[
          {field: 'id',type: 'checkbox', fixed: 'left'}
          ,{field: 'name', title: '分类'}
          ,{
            field: 'level', title: '级别', align: 'center', sort: true, totalRow: true, templet: function (d) {
              if (d.level == 1) {
                return '<span class="layui-badge layui-bg-danger">一级</span>';
              }
              if (d.level == 2) {
                return '<span class="layui-badge layui-bg-blue">二级</span>';
              }
              if (d.level == 3) {
                return '<span class="layui-badge layui-bg-gray">三级</span>';
              }
            }
          }
          ,{field: 'sort', title: '排序',align: 'center', sort: true, totalRow: true}
          ,{
            field: 'is_hot', width: 120, align: 'center', templet: function (d) {
              if (d.is_hot == 1) {
                return '<span class="layui-badge layui-bg-green">热门</span>';
              }
              if (d.is_hot == 0) {
                return '<span class="layui-badge layui-bg-gray">常规</span>';
              }
            }, title: '是否热门'
          }
          ,{field: 'create_time', title: '创建时间'}
          ,{title: '操作', templet: '#table-content-list', align: 'center'}
        ]],
        done: function () {
          layer.closeAll('loading');
        }
      });
    };
    renderTable();

    // 监听工具条
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var _id = data.id;
      if(obj.event === 'delete'){
        layer.confirm('确定删除该条记录吗？', {title : '温馨提示'}, function(){
          $.ajax({
            url:"{:url('job/categoryDelete')}",
            type:'post',
            data:{'id':_id},
            success: function(res){
              if(res.code == 0){
                obj.del();
                layer.msg(res.msg,{icon:1});
              } else{
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if(obj.event === 'edit'){
        layer.open({
          type: 2
          ,title: '编辑'
          ,content: "{:url('job/categoryEdit')}?id=" + _id
          ,maxmin: true
          ,area: ['85%', '85%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");
            iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
              var field = data.field;
              if(field.is_hot){
                field.is_hot = 1;
              } else {
                field.is_hot = 0;
              }
              $.ajax({
                url:"{:url('job/categoryEdit')}",
                type:'post',
                data:{'field': field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    layer.close(index);
                    //table.reload('LAY-app-content-list');
                  }
                  else{
                    layer.msg(res.msg,{icon:5});
                    return false;
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      }
    });

    var $ = layui.$, active = {
      insert: function(){
        layer.open({
          type: 2
          ,title: '添加岗位'
          ,content: "{:url('job/categoryInsert')}"
          ,maxmin: true
          ,shade:0.7
          ,shadeClose:true
          ,area: ['60%', '80%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-submit");
            iframeWindow.layui.form.on('submit(Lay-app-form-submit)', function(data){
              var field = data.field;
              if(field.is_hot){
                field.is_hot = 1;
              } else {
                field.is_hot = 0;
              }
              if(field.name == ''){ layer.msg('岗位名称不能为空',{icon:5});}
              $.ajax({
                url:"{:url('job/categoryInsert')}",
                type:'post',
                data:{'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    renderTable();
                    layer.close(index);
                  } else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
      },
      expand: function(){
        treetable.expandAll('#LAY-app-content-list');
      },
      fold: function(){
        treetable.foldAll('#LAY-app-content-list');
      },
    };

    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
{include file="public/footer" /}